<template>
  <div class='container'>
    <el-breadcrumb class="el-icon-arrow-right" separator-class="el-icon-arrow-right"  v-if="subject">
      <el-breadcrumb-item>学科管理</el-breadcrumb-item>
      <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
      <el-breadcrumb-item>目录管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form ref="formInline" :inline="true" :model="formInline" class="demo-form-inline">

      <el-form-item label="目录名称">
        <el-input v-model="formInline.directoryName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="formInline.state" placeholder="请选择">
        <el-option v-for="item in status" :key="item.value" :value="item.value" :label="item.label" ></el-option>
          <!-- <el-option label="启用" value="beijing"></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('formInline')" size="small">清除</el-button>
        <el-button type="primary" @click="onSubmit(formInline.directoryName)" size="small">搜索</el-button>
        <el-button class="subject" type="text" icon="el-icon-back" @click="fhBtn" v-if="subject">返回学科</el-button>
        <el-button class="el-icon-edit" type="success" size="small" @click="addDialog">新增目录</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-alert
      type="info"
      :closable="false"
      show-icon
      >
      数据一共{{total}}条
    </el-alert> -->
     <el-alert
    type="info"

    show-icon>
     数据一共{{total}}条
  </el-alert>

    <el-card style="margin-top: 10px;">
      <el-table :data="list" style="width: 100%" :index="indexMethod">
        <el-table-column width="75" label="序号" type="index" />
        <el-table-column width="170" label="所属学科" prop="subjectName" />
        <el-table-column width="170" label="目录名称" prop="directoryName" />
        <el-table-column width="170" label="创建者" prop="username" />
        <el-table-column width="170" label="创建日期" prop="addDate" />
        <el-table-column width="170" label="面试题数量" prop="totals" />
        <el-table-column width="160" label="状态" prop="state" :formatter="formatEmployment" />
        <el-table-column label="操作" fixed="right" width="150" >
          <template #default="{row}"  >
             <!-- #default="{row}"   @click="stopFrom" -->
          <el-button type="text"  @click="stopFrom(row,row.id)">{{row.state?"禁用":"启用"}}</el-button>
          <el-button type="text" @click="updateDialog(row,'2')" :disabled="row.disabled">修改</el-button>
          <el-button type="text" @click="removeDialog(row)" :disabled="row.disabled">删除</el-button>
          <!-- icon="el-icon-warning"  @click="editDialog" -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <div style="padding: 10px; display: flex; justify-content: flex-end">
        <el-pagination
        background
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :current-page="page"
        :page-sizes="[2, 5, 10, 30]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      </div>
    </el-card>
    <addDirectorys :showDialog="showDialog" @closeDialogFn="closeDialog " :formData="formData" @updata='updata'></addDirectorys>
  </div>
</template>

<script>
import { list, remove, detail } from '../../api/hmmm/directorys'
// import {list } from '../../api/hmmm/subjects'
import { status } from '../../api/hmmm/constants'
import addDirectorys from '../components/directorys-add.vue'
export default {
  data () {
    return {
      list: [],
      arr: [],
      formInline: {
        directoryName: '',
        state: ''
      },
      showDialog: false, // 弹窗隐藏
      total: 0, // 总数据
      data: {
        page: 1,
        pagesize: 5
      },
      page: 1,
      pagesize: 5,
      pages: '',
      status,
      formData: {
        subjectID: '',
        directoryName: ''
      },
      disabled: false,
      id: '',
      subject: false
    }
  },
  components: {
    addDirectorys
  },
  created () {
    if (this.$route.query.id) {
      this.subject = true
      this.getDetail()
    }
    this.listApi(this.data)
    console.log(this.$route.query)
  },
  computed: {
    getId() {
      return this.$route.query
    }
  },
  methods: {
    async  getDetail() {
      const res = await detail(this.$route.query)
      console.log(res)
    },
    // 搜索
    async onSubmit (val) {
      const arr = []
      if (!val) {
        this.$message('请输入关键字')
      } else {
        for (let i = 1; i <= this.pages; i++) {
          this.data.page = i
          const { data: { items } } = await list(this.data)
          this.arr = this.arr.concat(items)
          this.arr.forEach(item => {
          // item.directoryName
            if (item.directoryName.includes(val) && item.state == this.formInline.state) {
              console.log(1)

              arr.push(item)
            }
          })
        }
        this.list = arr
        this.total = this.list.length
        this.pagesize = this.list.length
        this.page = this.total / this.pagesize
      }
    },
    // 清除
    resetForm () {
      this.formInline = {
        directoryName: '',
        state: ''
      }
    },
    // 序号
    indexMethod (index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    async listApi (data) {
      const { data: { items, counts, pages } } = await list(data)
      this.total = counts
      // console.log(items)
      this.pages = pages
      this.list = items
      // this.arr = items
      const disabled = 'disabled'
      this.list.forEach(item => {
        item[disabled] = false
        if (item.state) {
          item.disabled = false
        } else {
          item.disabled = true
        }
      })
    },
    // 弹窗
    closeDialog () {
      this.showDialog = false
      this.formData = {
        subjectID: '',
        directoryName: ''
      }
    },
    // 分页
    handleSizeChange (val) {
      this.data.pagesize = val
      this.data.page = 1
      this.listApi(this.data)
    },
    handleCurrentChange (val) {
      this.data.page = val
      this.listApi(this.data)
    },

    // 格式化聘用形式
    formatEmployment (row, column, cellValue, index) {
      const obj = status.find(item => item.value === +cellValue)
      return obj ? obj.label : '未知'
    },
    // 禁用
    stopFrom (row, id) {
      const state = this.list.findIndex(item => item.id === id)
      this.list[state].state = !this.list[state].state
      this.list[state].disabled = !this.list[state].disabled

      console.log(row.state, this.disabled)
    },
    // editDialog () {},
    // 新增目录
    addDialog () {
      this.showDialog = true
    },
    // 删除
    removeDialog (row) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        if (this.list.length === 1 && this.page > 1) {
          this.page--
          this.data.page--
          this.$message.success('恭喜！删除成功')
          this.listApi(this.data)
        } else {
          this.$message.success('恭喜！删除成功')
          this.listApi(this.data)
        }
      }).catch(() => {})
    },
    // 修改
    updateDialog (row) {
      this.showDialog = true
      this.formData = row
    },
    updata () {
      this.listApi()
    },
    fhBtn() {
      this.$router.push('/subjects/list')
    }

  }
// 123Y
}
</script>

<style lang="scss" scoped>
.container {
  background-color: #fff;
  margin: 10px;
  padding: 10px;
  .el-icon-arrow-right{
    width: 100%;
    padding: 10px 15px 20px;
    border-bottom: 1px solid rgb(237, 234, 234);
  }
  .demo-form-inline{
    padding: 10px 10px 0 10px;
    .subject{
  margin-left:370px ;
}
    .el-icon-edit{
      margin-left:30px;
  }
  }

  height: 100%;
}
</style>
